package com.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.entity.User;
import com.group.mapper.UserMapper;
import com.group.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.vo.DataVO;
import com.group.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 朋心合力
 * @since 2021-08-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper mapper;


    @Override
    public User findByUsername(String username) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userName", username);
        User user = mapper.selectOne(wrapper);
        return user;
    }

    @Override
    public DataVO<UserVO> findData(Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");
        //分页
        IPage<User> iPage = new Page<>(page,limit);
        IPage<User> userIPage = mapper.selectPage(iPage, null);

        dataVO.setCount(userIPage.getTotal());
        List<User> userList = userIPage.getRecords();
        List<UserVO> userVOList = new ArrayList<>();
        for (User user : userList) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);
            userVOList.add(userVO);
        }
        dataVO.setData(userVOList);
        return dataVO;
    }


}
